﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.service.impl;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.util.*;



@Service
@Transactional
public class SearchProjectCvCrmServiceImpl extends BaseBusinessService<SearchProjectCvCrm, java.lang.Integer> implements SearchProjectCvCrmService<SearchProjectCvCrm, java.lang.Integer> {

	private SearchProjectCvCrmDao searchProjectCvCrmDao;

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写 */
	public void setSearchProjectCvCrmDaoImpl(SearchProjectCvCrmDao dao) {
		this.searchProjectCvCrmDao = dao;
	}

	private SearchProjectCvService searchProjectCvService;

	public void setSearchProjectCvServiceImpl(SearchProjectCvService service) {
		this.searchProjectCvService = service;
	}

	public EntityDao getEntityDao() {
		return this.searchProjectCvCrmDao;
	}

	@Transactional(readOnly = true)
	public Page findPage(SearchProjectCvCrmQuery query) {
		return searchProjectCvCrmDao.findPage(query);
	}

	public void save(SearchProjectCvCrm searchProjectCvCrm) {
		searchProjectCvCrmDao.save(searchProjectCvCrm);
		searchProjectCvCrmDao.flush();
		SearchProjectCv searchProjectCv = new SearchProjectCv();
		searchProjectCv.setCvId(searchProjectCvCrm.getCvId());
		searchProjectCv.setProjectId(searchProjectCvCrm.getProjectId());
		searchProjectCv.setStatus(SearchProjectCv.STATUS_LX);
		searchProjectCvService.updateProjectCvStatus(searchProjectCv);
	}

	public SearchProjectCvCrm getById(Integer id) {
		return (SearchProjectCvCrm) searchProjectCvCrmDao.getById(id);
	}

	@Override
	public void importCvCrmFromExcel(String path) {
		//
		try {
			//人选ID  联系记录
			String[] params = {"cvId","comment"};
			InputStream ips = new FileInputStream(path);
			// 工作表
			HSSFWorkbook wb = new HSSFWorkbook(ips);
			// 工作薄
			HSSFSheet sheet = wb.getSheetAt(0); // 获取第一个工作表

			for (int i = 1; sheet.getRow(i) != null; i++) {
				SearchProjectCvCrm crm = new SearchProjectCvCrm();
				crm.setCreator("waki.hu");
				crm.setCreateDt(new Date());
				crm.setProjectId(0); //
				crm.setCrmType(ConstantDic.ContactMethod_C); // 联系方式 默认为 电话/视频
				// 遍历单元格
				for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) {
					HSSFCell cell = sheet.getRow(i).getCell(j);
					if (cell != null) {
						if (BeanUtils.isExistsProperty(SearchProjectCvCrm.class, params[j])) {
							BeanUtils.setProperty(crm, params[j],ExcelHelper.getStringValue(cell));
						}
					}
				}
				//单元取值完毕
				this.save(crm);
				log.info("第"+i+"个联系记录导入成功!人选编号为:"+crm.getCvId()+";联系记录ID:"+crm.getId());
				
				/*if(i>2){
					break;
				}*/
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
